﻿using System;
using UnityEngine;
using UnityEngine.UIElements;
using winS.UnityEditor.UIElement;

namespace winS.UnityEditor.ProcessEditor
{
    public class SettingBar : VisualElement
    {
        private static readonly Color backgroundColor = ColorLevelUtility.GetColor(5);

        public event Action onSave;

        public SettingBar(ProcessEditorData processEditorData)
        {
            style.SetMinHeight(24f);
            style.flexShrink = 0f;
            style.flexDirection = FlexDirection.Row;
            style.SetBackgroundColor(backgroundColor);

            style.SetBorderTop(1f, EditorElementUtility.defaultSplitlineColor);

            SwitchButton saveOnCloseButton = EditorElementUtility.elementFactory.CreateSwitchButton("关闭时保存", processEditorData.saveOnClose, newValue => processEditorData.saveOnClose = newValue);
            SetUniversalStyle(saveOnCloseButton);
            saveOnCloseButton.SetColor(backgroundColor, 1.4f);

            Add(EditorElementUtility.elementFactory.CreateVerticalSplitline());

            SwitchButton saveOnSwitchButton = EditorElementUtility.elementFactory.CreateSwitchButton("切换时保存", processEditorData.saveOnSwitch, newValue => processEditorData.saveOnSwitch = newValue);
            SetUniversalStyle(saveOnSwitchButton);
            saveOnSwitchButton.SetColor(backgroundColor, 1.4f);

            Add(EditorElementUtility.elementFactory.CreateVerticalSplitline());
            Add(EditorElementUtility.elementFactory.CreateRow(1f));
            Add(EditorElementUtility.elementFactory.CreateVerticalSplitline());

            SwitchButton saveButton = EditorElementUtility.elementFactory.CreateSwitchButton(Resources.Load<Texture>("winS.UnityEditor.ProcessEditor/Icons/保存"), 14f, "保存", false, _ => onSave?.Invoke());
            SetUniversalStyle(saveButton);
            saveButton.onColor = saveButton.offColor = new Color32(0, 157, 255, 255);
            saveButton.SetColor(backgroundColor, 1.4f);

            void SetUniversalStyle(SwitchButton switchButton)
            {
                Add(switchButton);
                switchButton.style.ClearBorderTop();
                switchButton.style.ClearBorderLeft();
                switchButton.style.ClearBorderRight();

                switchButton.style.SetMargin(0f);
                switchButton.style.paddingLeft = switchButton.style.paddingRight = 4f;
            }
        }
    }
}